package com.xywy.beautyand.ACT;

import java.util.Calendar;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.beautyand.R;
import com.xywy.beautyand.base.BaseActivity;
import com.xywy.beautyand.bean.User;
import com.xywy.beautyand.util.Constants;
import com.xywy.beautyand.util.MD5;
import com.xywy.beautyand.util.UIUtil;
import com.xywy.beautyand.vollery.OnVolleyResponseListener;
import com.xywy.beautyand.vollery.PostRequest;

public class PersonDataAct extends BaseActivity implements OnClickListener {

	private RelativeLayout left_img;
	private TextView middle_title;
	// private TextView right_title;
	private LinearLayout tobar;
	private ImageView right_img;
	private RelativeLayout person_data_name_rel;
	private RelativeLayout setting_gender_relat;
	private RelativeLayout setting_height_rel;
	private RelativeLayout setting_weight_relat;
	private RelativeLayout setting_time_relat;

	private TextView tv_user_name;
	private TextView tv_user_sex;
	private TextView tv_user_height;
	private TextView tv_user_weight;
	private TextView tv_user_birthday;
	private User user;
	private SharedPreferences sp;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_person_data);
		user = User.getInstance(context);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		// right_title = (TextView) findViewById(R.id.right_title);
		right_img = (ImageView) findViewById(R.id.right_img);
		middle_title.setText("个人中心");
		person_data_name_rel = (RelativeLayout) findViewById(R.id.person_data_name_rel);
		setting_gender_relat = (RelativeLayout) findViewById(R.id.setting_gender_relat);
		setting_height_rel = (RelativeLayout) findViewById(R.id.setting_height_rel);
		setting_weight_relat = (RelativeLayout) findViewById(R.id.setting_weight_relat);
		setting_time_relat = (RelativeLayout) findViewById(R.id.setting_time_relat);

		tv_user_name = (TextView) findViewById(R.id.person_data_name);
		tv_user_sex = (TextView) findViewById(R.id.setting_gender);
		tv_user_height = (TextView) findViewById(R.id.setting_height);
		tv_user_weight = (TextView) findViewById(R.id.setting_weight);
		tv_user_birthday = (TextView) findViewById(R.id.set_time);
	}

	private void initValue() {
		sp = getSharedPreferences("config", MODE_PRIVATE);
		int weight = sp.getInt("weight", 0);
		User user = User.getInstance(context);
		tv_user_name.setText(user.getAccountstr());
		tv_user_sex.setText(user.getSex());
		tv_user_birthday.setText(user.getBirthday());
		tv_user_height.setText(user.getHeight());
		if (weight == 0) {
			tv_user_weight.setText("未设置");
		} else {
			tv_user_weight.setText(weight + "");
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initValue();
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		person_data_name_rel.setOnClickListener(this);
		setting_gender_relat.setOnClickListener(this);
		setting_height_rel.setOnClickListener(this);
		setting_weight_relat.setOnClickListener(this);
		setting_time_relat.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		// setTopbarOperation(R.drawable.back_img, "��������", -1);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		Intent updateIntent = new Intent(this, PersonDataUpdate.class);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		// case R.id.right_title:
		// Intent intent = new Intent(PersonDataAct.this, LoginAct.class);
		// startActivity(intent);
		// break;
		case R.id.person_data_name_rel:
			updateIntent.putExtra("intent_for_update",
					PersonDataUpdate.NAME_UPDATE);
			startActivity(updateIntent);
			break;
		case R.id.setting_gender_relat:
			updateIntent.putExtra("intent_for_update",
					PersonDataUpdate.SEX_UPDATE);
			startActivity(updateIntent);
			break;
		case R.id.setting_height_rel:
			updateIntent.putExtra("intent_for_update",
					PersonDataUpdate.HEIGHT_UPDATE);
			startActivity(updateIntent);
			break;
		case R.id.setting_weight_relat:
			updateIntent.putExtra("intent_for_update",
					PersonDataUpdate.WEIGHT_UPDATE);
			startActivity(updateIntent);
			break;
		case R.id.setting_time_relat:
			if (User.getInstance(context).getBirthday().length() > 0)
				showDateDialog();
			else {
				UIUtil.showToast(context, "未设置");
			}
			break;
		default:
			break;
		}
	}

	String birthDayStr;

	private void showDateDialog() {
		String dateStr = User.getInstance(context).getBirthday();
		String dateTemp[] = dateStr.split("-");
		int year = Integer.parseInt(dateTemp[0]);
		int mounth = Integer.parseInt(dateTemp[1]);
		int day = Integer.parseInt(dateTemp[2]);
		DatePickerDialog dp = new DatePickerDialog(this,
				new OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker dp, int year, int mounth,
							int day) {
						// TODO Auto-generated method stub
						birthDayStr = year + "-" + mounth + "-" + day;
						user.setBirthday(birthDayStr);
						changeUserInfo();
					}
				}, year, mounth, day);
		long currentDate = System.currentTimeMillis();
		dp.getDatePicker().setMaxDate(currentDate);
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, 1940);
		long minDate = calendar.getTimeInMillis();
		dp.getDatePicker().setMinDate(minDate);
		dp.show();
	}

	private void changeUserInfo() {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userid", user.getUserid());
		params.put("accountstr", user.getAccountstr());
		params.put("sex", user.getSex());
		params.put("height", user.getHeight());
		params.put("weight", 65 + "");
		params.put("birthday", user.getBirthday());
		params.put("keyword", "edit");
		params.put("tag", Constants.wjkTag);
		String sign;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.wjkTag);
			params.put("sign", sign);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PostRequest<String> request = new PostRequest<String>(
				PersonDataUpdate.updateUrl, String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						System.out.println(volleyError.getCause());
					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						try {
							System.out.println(response);
							JSONObject json = new JSONObject(response);
							int sate = json.optInt("state");
							if (sate == 200) {
								UIUtil.DissDialog(context, waittingDialog);
								user.setBirthday(birthDayStr);
								// User.getInstance(context).setUser(user);
								// UserKeeper.saveUserInfo(user,
								// PersonDataAct.this);
								tv_user_birthday.setText(birthDayStr);
							} else {
								Toast.makeText(PersonDataAct.this, "更新失败",
										Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
		request.setIsParseJson(false);
		request.setParams(params);
		executeRequest(request);
	}
}
